home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libio / iostream.cc < prev    next >
C/C++ Source or Header  |  1994-10-13  |  18KB  |  825 lines

  1. /* This is part of libio/iostream, providing -*- C++ -*- input/output.
  2. Copyright (C) 1993 Free Software Foundation
  3.  
  4. This file is part of the GNU IO Library.  This library is free
  5. software; you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the
  7. Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU CC; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. As a special exception, if you link this library with files
  20. compiled with a GNU compiler to produce an executable, this does not cause
  21. the resulting executable to be covered by the GNU General Public License.
  22. This exception does not however invalidate any other reasons why
  23. the executable file might be covered by the GNU General Public License. */
  24.  
  25. /* Written by Per Bothner (bothner@cygnus.com). */
  26.  
  27. #ifdef __GNUC__
  28. #pragma implementation
  29. #endif
  30. #define _STREAM_COMPAT
  31. #include <iostream.h>
  32. #include "libioP.h"
  33. #include <stdio.h>  /* Needed for sprintf */
  34. #include <ctype.h>
  35. #include <string.h>
  36. #include <limits.h>
  37. #include "floatio.h"
  38.  
  39. #define    BUF        (MAXEXP+MAXFRACT+1)    /* + decimal point */
  40.  
  41. //#define isspace(ch) ((ch)==' ' || (ch)=='\t' || (ch)=='\n')
  42.  
  43. istream::istream(streambuf *sb, ostream* tied)
  44. {
  45.   init (sb, tied);
  46.   _flags |= ios::dont_close;
  47.   _gcount = 0;
  48. }
  49.  
  50. int skip_ws(streambuf* sb)
  51. {
  52.     int ch;
  53.     for (;;) {
  54.     ch = sb->sbumpc();
  55.     if (ch == EOF || !isspace(ch))
  56.         return ch;
  57.     }
  58. }
  59.  
  60. istream& istream::get(char& c)
  61. {
  62.     if (ipfx1()) {
  63.     int ch = _strbuf->sbumpc();
  64.     if (ch == EOF) {
  65.       set(ios::eofbit|ios::failbit);
  66.       _gcount = 0;
  67.     }
  68.     else {
  69.       c = (char)ch;
  70.       _gcount = 1;
  71.     }
  72.     }
  73.     else
  74.       _gcount = 0;
  75.     return *this;
  76. }
  77.  
  78. int istream::peek()
  79. {
  80.   if (!good())
  81.     return EOF;
  82.   if (_tie && rdbuf()->in_avail() == 0)
  83.     _tie->flush();
  84.   int ch = _strbuf->sgetc();
  85.   if (ch == EOF)
  86.     set(ios::eofbit);
  87.   return ch;
  88. }
  89.  
  90. istream& istream::ignore(int n /* = 1 */, int delim /* = EOF */)
  91. {
  92.     _gcount = 0;
  93.     if (ipfx1()) {
  94.     register streambuf* sb = _strbuf;
  95.     if (delim == EOF) {
  96.         _gcount = sb->ignore(n);
  97.         return *this;
  98.     }
  99.     for (;;) {
  100. #if 0
  101.         if (n != MAXINT) // FIXME
  102. #endif
  103.         if (--n < 0)
  104.         break;
  105.         int ch = sb->sbumpc();
  106.         if (ch == EOF) {
  107.         set(ios::eofbit|ios::failbit);
  108.         break;
  109.         }
  110.         _gcount++;
  111.         if (ch == delim)
  112.         break;
  113.     }
  114.     }
  115.     return *this;
  116. }
  117.  
  118. istream& istream::read(char *s, int n)
  119. {
  120.     if (ipfx1()) {
  121.     _gcount = _strbuf->sgetn(s, n);
  122.     if (_gcount != n)
  123.         set(ios::failbit|ios::eofbit);
  124.     }
  125.     else
  126.       _gcount = 0;
  127.     return *this;
  128. }
  129.  
  130. istream& istream::seekg(streampos pos)
  131. {
  132.     pos = _strbuf->sseekpos(pos, ios::in);
  133.     if (pos == streampos(EOF))
  134.     set(ios::badbit);
  135.     return *this;
  136. }
  137.  
  138. istream& istream::seekg(streamoff off, _seek_dir dir)
  139. {
  140.   streampos pos
  141.     = _IO_seekoff (_strbuf, off,
  142.            (_IO_seekflags)
  143.            ((int)dir | _IO_seek_not_out | _IO_seek_pos_ignored));
  144.   if (pos == streampos(EOF))
  145.     set(ios::badbit);
  146.   return *this;
  147. }
  148.  
  149. streampos istream::tellg()
  150. {
  151. #if 0
  152.     streampos pos = _strbuf->sseekoff(0, ios::cur, ios::in);
  153. #else
  154.     streampos pos
  155.       = _IO_seekoff (_strbuf, 0,
  156.              (_IO_seekflags)(_IO_seek_cur | _IO_seek_not_out));
  157. #endif
  158.     if (pos == streampos(EOF))
  159.     set(ios::badbit);
  160.     return pos;
  161. }
  162.  
  163. istream& istream::operator>>(char& c)
  164. {
  165.     if (ipfx0()) {
  166.     int ch = _strbuf->sbumpc();
  167.     if (ch == EOF)
  168.         set(ios::eofbit|ios::failbit);
  169.     else
  170.         c = (char)ch;
  171.     }
  172.     return *this;
  173. }
  174.  
  175. istream& istream::operator>>(char* ptr)
  176. {
  177.   register char *p = ptr;
  178.   int w = width(0);
  179.   if (ipfx0()) {
  180.     register streambuf* sb = _strbuf;
  181.     for (;;)
  182.       {
  183.     int ch = sb->sbumpc();
  184.     if (ch == EOF)
  185.       {
  186.         set(p == ptr ? (ios::eofbit|ios::failbit) : (ios::eofbit));
  187.         break;
  188.       }
  189.     else if (isspace(ch))
  190.       {
  191.         sb->sputbackc(ch);
  192.         break;
  193.       }
  194.     else if (w == 1)
  195.       {
  196.         set(ios::failbit);
  197.         sb->sputbackc(ch);
  198.         break;
  199.       }
  200.     else *p++ = ch;
  201.     w--;
  202.       }
  203.   }
  204.   *p = '\0';
  205.   return *this;
  206. }
  207.  
  208. #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
  209. #define LONGEST long long
  210. #else
  211. #define LONGEST long
  212. #endif
  213.  
  214. static int read_int(istream& stream, unsigned LONGEST& val, int& neg)
  215. {
  216.     if (!stream.ipfx0())
  217.       return 0;
  218.     register streambuf* sb = stream.rdbuf();
  219.     int base = 10;
  220.     int ndigits = 0;
  221.     register int ch = skip_ws(sb);
  222.     if (ch == EOF)
  223.     goto eof_fail;
  224.     neg = 0;
  225.     if (ch == '+') {
  226.     ch = skip_ws(sb);
  227.     }
  228.     else if (ch == '-') {
  229.     neg = 1;
  230.     ch = skip_ws(sb);
  231.     }
  232.     if (ch == EOF) goto eof_fail;
  233.     if (!(stream.flags() & ios::basefield)) {
  234.     if (ch == '0') {
  235.         ch = sb->sbumpc();
  236.         if (ch == EOF) {
  237.         val = 0;
  238.         return 1;
  239.         }
  240.         if (ch == 'x' || ch == 'X') {
  241.         base = 16;
  242.         ch = sb->sbumpc();
  243.         if (ch == EOF) goto eof_fail;
  244.         }
  245.         else {
  246.         sb->sputbackc(ch);
  247.         base = 8;
  248.         ch = '0';
  249.         }
  250.     }
  251.     }
  252.     else if ((stream.flags() & ios::basefield) == ios::hex)
  253.     base = 16;
  254.     else if ((stream.flags() & ios::basefield) == ios::oct)
  255.     base = 8;
  256.     val = 0;
  257.     for (;;) {
  258.     if (ch == EOF)
  259.         break;
  260.     int digit;
  261.     if (ch >= '0' && ch <= '9')
  262.         digit = ch - '0';
  263.     else if (ch >= 'A' && ch <= 'F')
  264.         digit = ch - 'A' + 10;
  265.     else if (ch >= 'a' && ch <= 'f')
  266.         digit = ch - 'a' + 10;
  267.     else
  268.         digit = 999;
  269.     if (digit >= base) {
  270.         sb->sputbackc(ch);
  271.         if (ndigits == 0)
  272.         goto fail;
  273.         else
  274.         return 1;
  275.     }
  276.     ndigits++;
  277.     val = base * val + digit;
  278.     ch = sb->sbumpc();
  279.     }
  280.     return 1;
  281.   fail:
  282.     stream.set(ios::failbit);
  283.     return 0;
  284.   eof_fail:
  285.     stream.set(ios::failbit|ios::eofbit);
  286.     return 0;
  287. }
  288.  
  289. #define READ_INT(TYPE) \
  290. istream& istream::operator>>(TYPE& i)\
  291. {\
  292.     unsigned LONGEST val; int neg;\
  293.     if (read_int(*this, val, neg)) {\
  294.     if (neg) val = -val;\
  295.     i = (TYPE)val;\
  296.     }\
  297.     return *this;\
  298. }
  299.  
  300. READ_INT(short)
  301. READ_INT(unsigned short)
  302. READ_INT(int)
  303. READ_INT(unsigned int)
  304. READ_INT(long)
  305. READ_INT(unsigned long)
  306. #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
  307. READ_INT(long long)
  308. READ_INT(unsigned long long)
  309. #endif
  310. #if _G_HAVE_BOOL
  311. READ_INT(bool)
  312. #endif
  313.  
  314. istream& istream::operator>>(long double& x)
  315. {
  316.     if (ipfx0())
  317.     scan("%lg", &x);
  318.     return *this;
  319. }
  320.  
  321. istream& istream::operator>>(double& x)
  322. {
  323.     if (ipfx0())
  324.     scan("%lg", &x);
  325.     return *this;
  326. }
  327.  
  328. istream& istream::operator>>(float& x)
  329. {
  330.     if (ipfx0())
  331.     scan("%g", &x);
  332.     return *this;
  333. }
  334.  
  335. istream& istream::operator>>(register streambuf* sbuf)
  336. {
  337.     if (ipfx0()) {
  338.     register streambuf* inbuf = rdbuf();
  339.     // FIXME: Should optimize!
  340.     for (;;) {
  341.         register int ch = inbuf->sbumpc();
  342.         if (ch == EOF) {
  343.         set(ios::eofbit);
  344.         break;
  345.         }
  346.         if (sbuf->sputc(ch) == EOF) {
  347.         set(ios::failbit);
  348.         break;
  349.         }
  350.     }
  351.     }
  352.     return *this;
  353. }
  354.  
  355. ostream& ostream::operator<<(char c)
  356. {
  357.     if (opfx()) {
  358. #if 1
  359.     // This is what the cfront implementation does.
  360.     if (_strbuf->sputc(c) == EOF)
  361.       goto failed;
  362. #else
  363.     // This is what cfront documentation and current ANSI drafts say.
  364.     int w = width(0);
  365.     char fill_char = fill();
  366.     register int padding = w > 0 ? w - 1 : 0;
  367.     register streambuf *sb = _strbuf;
  368.     if (!(flags() & ios::left) && padding) // Default adjustment.
  369.         if (_IO_padn(sb, fill_char, padding) < padding)
  370.           goto failed;
  371.     if (sb->sputc(c) == EOF)
  372.       goto failed;
  373.     if (flags() & ios::left && padding) // Left adjustment.
  374.         if (_IO_padn(sb, fill_char, padding) < padding)
  375.           goto failed;
  376. #endif
  377.     osfx();
  378.     }
  379.     return *this;
  380.   failed:
  381.     set(ios::badbit);
  382.     osfx();
  383.     return *this;
  384. }
  385.  
  386. /* Write VAL on STREAM.
  387.    If SIGN<0, val is the absolute value of a negative number.
  388.    If SIGN>0, val is a signed non-negative number.
  389.    If SIGN==0, val is unsigned. */
  390.  
  391. static void write_int(ostream& stream, unsigned LONGEST val, int sign)
  392. {
  393. #define WRITE_BUF_SIZE (10 + sizeof(unsigned LONGEST) * 3)
  394.     char buf[WRITE_BUF_SIZE];
  395.     register char *buf_ptr = buf+WRITE_BUF_SIZE; // End of buf.
  396.     char *show_base = "";
  397.     int show_base_len = 0;
  398.     int show_pos = 0; // If 1, print a '+'.
  399.  
  400.     // Now do the actual conversion, placing the result at the *end* of buf.
  401.     // Note that we use separate code for decimal, octal, and hex,
  402.     // so we can divide by optimizable constants.
  403.     if ((stream.flags() & ios::basefield) == ios::oct) { // Octal
  404.     do {
  405.         *--buf_ptr = (val & 7) + '0';
  406.         val = val >> 3;
  407.     } while (val != 0);
  408.     if ((stream.flags() & ios::showbase) && (*buf_ptr != '0'))
  409.         *--buf_ptr = '0';
  410.     }
  411.     else if ((stream.flags() & ios::basefield) == ios::hex) { // Hex
  412.     char *xdigs = (stream.flags() & ios::uppercase) ? "0123456789ABCDEF0X"
  413.         : "0123456789abcdef0x";
  414.     do {
  415.         *--buf_ptr = xdigs[val & 15];
  416.         val = val >> 4;
  417.     } while (val != 0);
  418.     if ((stream.flags() & ios::showbase)) {
  419.         show_base = xdigs + 16; // Either "0X" or "0x".
  420.         show_base_len = 2;
  421.     }
  422.     }
  423.     else { // Decimal
  424. #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
  425.     // Optimization:  Only use long long when we need to.
  426.     while (val > UINT_MAX) {
  427.         *--buf_ptr = (val % 10) + '0';
  428.         val /= 10;
  429.     }
  430.     // Use more efficient (int) arithmetic for the rest.
  431.     register unsigned int ival = (unsigned int)val;
  432. #else
  433.     register unsigned LONGEST ival = val;
  434. #endif
  435.     do {
  436.         *--buf_ptr = (ival % 10) + '0';
  437.         ival /= 10;
  438.     } while (ival != 0);
  439.     if (sign > 0 && (stream.flags() & ios::showpos))
  440.         show_pos=1;
  441.     }
  442.  
  443.     int buf_len = buf+WRITE_BUF_SIZE - buf_ptr;
  444.     int w = stream.width(0);
  445.  
  446.     // Calculate padding.
  447.     int len = buf_len+show_pos;
  448.     if (sign < 0) len++;
  449.     len += show_base_len;
  450.     int padding = len > w ? 0 : w - len;
  451.  
  452.     // Do actual output.
  453.     register streambuf* sbuf = stream.rdbuf();
  454.     ios::fmtflags pad_kind =
  455.     stream.flags() & (ios::left|ios::right|ios::internal);
  456.     char fill_char = stream.fill();
  457.     if (padding > 0
  458.     && pad_kind != (ios::fmtflags)ios::left
  459.     && pad_kind != (ios::fmtflags)ios::internal) // Default (right) adjust.
  460.     if (_IO_padn(sbuf, fill_char, padding) < padding)
  461.       goto failed;
  462.     if (sign < 0 || show_pos)
  463.       {
  464.     char ch = sign < 0 ? '-' : '+';
  465.     if (sbuf->sputc(ch) < 0)
  466.       goto failed;
  467.       }
  468.     if (show_base_len)
  469.     if (_IO_sputn(sbuf, show_base, show_base_len) <= 0)
  470.       goto failed;
  471.     if (pad_kind == (ios::fmtflags)ios::internal && padding > 0)
  472.       if (_IO_padn(sbuf, fill_char, padding) < padding)
  473.     goto failed;
  474.     if (_IO_sputn (sbuf, buf_ptr, buf_len) != buf_len)
  475.       goto failed;
  476.     if (pad_kind == (ios::fmtflags)ios::left && padding > 0) // Left adjustment
  477.       if (_IO_padn(sbuf, fill_char, padding) < padding)
  478.     goto failed;
  479.     stream.osfx();
  480.     return;
  481.   failed:
  482.     stream.set(ios::badbit);
  483.     stream.osfx();
  484. }
  485.  
  486. ostream& ostream::operator<<(int n)
  487. {
  488.     if (opfx()) {
  489.     int sign = 1;
  490.     unsigned int abs_n = (unsigned)n;
  491.     if (n < 0 && (flags() & (ios::oct|ios::hex)) == 0)
  492.         abs_n = -((unsigned)n), sign = -1;
  493.     write_int(*this, abs_n, sign);
  494.     }
  495.     return *this;
  496. }
  497.  
  498. ostream& ostream::operator<<(unsigned int n)
  499. {
  500.     if (opfx())
  501.     write_int(*this, n, 0);
  502.     return *this;
  503. }
  504.  
  505.  
  506. ostream& ostream::operator<<(long n)
  507. {
  508.     if (opfx()) {
  509.     int sign = 1;
  510.     unsigned long abs_n = (unsigned long)n;
  511.     if (n < 0 && (flags() & (ios::oct|ios::hex)) == 0)
  512.         abs_n = -((unsigned long)n), sign = -1;
  513.     write_int(*this, abs_n, sign);
  514.     }
  515.     return *this;
  516. }
  517.  
  518. ostream& ostream::operator<<(unsigned long n)
  519. {
  520.     if (opfx())
  521.     write_int(*this, n, 0);
  522.     return *this;
  523. }
  524.  
  525. #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
  526. ostream& ostream::operator<<(long long n)
  527. {
  528.     if (opfx()) {
  529.     int sign = 1;
  530.     unsigned long long abs_n = (unsigned long long)n;
  531.     if (n < 0 && (flags() & (ios::oct|ios::hex)) == 0)
  532.         abs_n = -((unsigned long long)n), sign = -1;
  533.     write_int(*this, abs_n, sign);
  534.     }
  535.     return *this;
  536. }
  537.  
  538.  
  539. ostream& ostream::operator<<(unsigned long long n)
  540. {
  541.     if (opfx())
  542.     write_int(*this, n, 0);
  543.     return *this;
  544. }
  545. #endif /*__GNUC__*/
  546.  
  547. ostream& ostream::operator<<(double n)
  548. {
  549.     if (opfx()) {
  550.     // Uses __cvt_double (renamed from static cvt), in Chris Torek's
  551.     // stdio implementation.  The setup code uses the same logic
  552.     // as in __vsbprintf.C (also based on Torek's code).
  553.     int format_char;
  554.     if ((flags() & ios::floatfield) == ios::fixed)
  555.         format_char = 'f';
  556.     else if ((flags() & ios::floatfield) == ios::scientific)
  557.         format_char = flags() & ios::uppercase ? 'E' : 'e';
  558.     else
  559.         format_char = flags() & ios::uppercase ? 'G' : 'g';
  560.  
  561.     int prec = precision();
  562.     if (prec <= 0 && !(flags() & ios::fixed))
  563.       prec = 6; /* default */
  564.  
  565.     // Do actual conversion.
  566. #ifdef _IO_USE_DTOA
  567.     if (_IO_outfloat(n, rdbuf(), format_char, width(0),
  568.              prec, flags(),
  569.              flags() & ios::showpos ? '+' : 0,
  570.              fill()) < 0)
  571.         set(ios::badbit|ios::failbit); // ??
  572. #else
  573.     int fpprec = 0; // 'Extra' (suppressed) floating precision.
  574.     if (prec > MAXFRACT) {
  575.         if (flags() & (ios::fixed|ios::scientific) & ios::showpos)
  576.         fpprec = prec - MAXFRACT;
  577.         prec = MAXFRACT;
  578.     }
  579.     int negative;
  580.     char buf[BUF];
  581.     int sign = '\0';
  582.     char *cp = buf;
  583.     *cp = 0;
  584.     int size = __cvt_double(n, prec,
  585.                 flags() & ios::showpoint ? 0x80 : 0,
  586.                 &negative,
  587.                 format_char, cp, buf + sizeof(buf));
  588.     if (negative) sign = '-';
  589.     else if (flags() & ios::showpos) sign = '+';
  590.     if (*cp == 0)
  591.         cp++;
  592.  
  593.     // Calculate padding.
  594.     int fieldsize = size + fpprec;
  595.     if (sign) fieldsize++;
  596.     int padding = 0;
  597.     int w = width(0);
  598.     if (fieldsize < w)
  599.         padding = w - fieldsize;
  600.  
  601.     // Do actual output.
  602.     register streambuf* sbuf = rdbuf();
  603.     register i;
  604.     char fill_char = fill();
  605.     ios::fmtflags pad_kind =
  606.         flags() & (ios::left|ios::right|ios::internal);
  607.     if (pad_kind != (ios::fmtflags)ios::left // Default (right) adjust.
  608.         && pad_kind != (ios::fmtflags)ios::internal)
  609.         for (i = padding; --i >= 0; ) sbuf->sputc(fill_char);
  610.     if (sign)
  611.         sbuf->sputc(sign);
  612.     if (pad_kind == (ios::fmtflags)ios::internal)
  613.         for (i = padding; --i >= 0; ) sbuf->sputc(fill_char);
  614.     
  615.     // Emit the actual concented field, followed by extra zeros.
  616.     _IO_sputn (sbuf, cp, size);
  617.     for (i = fpprec; --i >= 0; ) sbuf->sputc('0');
  618.  
  619.     if (pad_kind == (ios::fmtflags)ios::left) // Left adjustment
  620.         for (i = padding; --i >= 0; ) sbuf->sputc(fill_char);
  621. #endif
  622.     osfx();
  623.     }
  624.     return *this;
  625. }
  626.  
  627. ostream& ostream::operator<<(const char *s)
  628. {
  629.   if (opfx())
  630.     {
  631.       if (s == NULL)
  632.     s = "(null)";
  633.       int len = strlen(s);
  634.       int w = width(0);
  635. // FIXME: Should we: if (w && len>w) len = w;
  636.       char fill_char = fill();
  637.       register streambuf *sbuf = rdbuf();
  638.       register int padding = w > len ? w - len : 0;
  639.       if (!(flags() & ios::left) && padding > 0) // Default adjustment.
  640.     if (_IO_padn(sbuf, fill_char, padding) != padding)
  641.       goto failed;
  642.       if (_IO_sputn (sbuf, s, len) != len)
  643.     goto failed;
  644.       if (flags() & ios::left && padding > 0) // Left adjustment.
  645.     if (_IO_padn(sbuf, fill_char, padding) != padding)
  646.       goto failed;
  647.       osfx();
  648.     }
  649.   return *this;
  650.  failed:
  651.   set(ios::badbit);
  652.   osfx();
  653.   return *this;
  654. }
  655.  
  656. #if 0
  657. ostream& ostream::operator<<(const void *p)
  658. { Is in osform.cc, to avoid pulling in all of _IO_vfprintf by this file. */ }
  659. #endif
  660.  
  661. ostream& ostream::operator<<(register streambuf* sbuf)
  662. {
  663.   if (opfx())
  664.     {
  665.       char buffer[_IO_BUFSIZ];
  666.       register streambuf* outbuf = _strbuf;
  667.       for (;;)
  668.     {
  669.       _IO_size_t count = _IO_sgetn(sbuf, buffer, _IO_BUFSIZ);
  670.       if (count <= 0)
  671.         break;
  672.       if (_IO_sputn(outbuf, buffer, count) != count)
  673.         {
  674.           set(ios::badbit);
  675.           break;
  676.         }
  677.     }
  678.       osfx();
  679.     }
  680.   return *this;
  681. }
  682.  
  683. ostream::ostream(streambuf* sb, ostream* tied)
  684. {
  685.   init (sb, tied);
  686.   _flags |= ios::dont_close;
  687. }
  688.  
  689. ostream& ostream::seekp(streampos pos)
  690. {
  691.     pos = _strbuf->sseekpos(pos, ios::out);
  692.     if (pos == streampos(EOF))
  693.     set(ios::badbit);
  694.     return *this;
  695. }
  696.  
  697. ostream& ostream::seekp(streamoff off, _seek_dir dir)
  698. {
  699.   streampos pos
  700.     = _IO_seekoff (_strbuf, off,
  701.            (_IO_seekflags)
  702.            ((int)dir | _IO_seek_not_in | _IO_seek_pos_ignored));
  703.   if (pos == streampos(EOF))
  704.     set(ios::badbit);
  705.   return *this;
  706. }
  707.  
  708. streampos ostream::tellp()
  709. {
  710. #if 1
  711.     streampos pos
  712.       = _IO_seekoff (_strbuf, 0,
  713.              (_IO_seekflags)(_IO_seek_cur | _IO_seek_not_in));
  714. #else
  715.     streampos pos = _strbuf->sseekoff(0, ios::cur, ios::out);
  716. #endif
  717.     if (pos == streampos(EOF))
  718.     set(ios::badbit);
  719.     return pos;
  720. }
  721.  
  722. ostream& ostream::flush()
  723. {
  724.     if (_strbuf->sync())
  725.     set(ios::badbit);
  726.     return *this;
  727. }
  728.  
  729. ostream& flush(ostream& outs)
  730. {
  731.   return outs.flush();
  732. }
  733.  
  734. istream& ws(istream& ins)
  735. {
  736.     if (ins.ipfx1()) {
  737.     int ch = skip_ws(ins._strbuf);
  738.     if (ch == EOF)
  739.         ins.set(ios::eofbit);
  740.     else
  741.         ins._strbuf->sputbackc(ch);
  742.     }
  743.     return ins;
  744. }
  745.  
  746. // Skip white-space.  Return 0 on failure (EOF), or 1 on success.
  747. // Differs from ws() manipulator in that failbit is set on EOF.
  748. // Called by ipfx() and ipfx0() if needed.
  749.  
  750. int istream::_skip_ws()
  751. {
  752.     int ch = skip_ws(_strbuf);
  753.     if (ch == EOF) {
  754.     set(ios::eofbit|ios::failbit);
  755.     return 0;
  756.     }
  757.     else {
  758.     _strbuf->sputbackc(ch);
  759.     return 1;
  760.     }
  761. }
  762.  
  763. ostream& ends(ostream& outs)
  764. {
  765.     outs.put('\0');
  766.     return outs;
  767. }
  768.  
  769. ostream& endl(ostream& outs)
  770. {
  771.     return flush(outs.put('\n'));
  772. }
  773.  
  774. ostream& ostream::write(const char *s, int n)
  775. {
  776.     if (opfx()) {
  777.     if (_IO_sputn(_strbuf, s, n) != n)
  778.         set(ios::failbit);
  779.     }
  780.     return *this;
  781. }
  782.  
  783. void ostream::do_osfx()
  784. {
  785.     if (flags() & ios::unitbuf)
  786.     flush();
  787.     if (flags() & ios::stdio) {
  788.     fflush(stdout);
  789.     fflush(stderr);
  790.     }
  791. }
  792.  
  793. iostream::iostream(streambuf* sb, ostream* tied)
  794. {
  795.   init (sb, tied);
  796.   _flags |= ios::dont_close;
  797. }
  798.  
  799. // NOTE: extension for compatibility with old libg++.
  800. // Not really compatible with fistream::close().
  801. #ifdef _STREAM_COMPAT
  802. void ios::close()
  803. {
  804.   if (!(_flags & (unsigned int)ios::dont_close))
  805.     delete rdbuf();
  806.   else if (_strbuf->_flags & _IO_IS_FILEBUF)
  807.     ((struct filebuf*)rdbuf())->close();
  808.   else if (_strbuf != NULL)
  809.     rdbuf()->sync();
  810.   _flags |= ios::dont_close;
  811.   _strbuf = NULL;
  812.   _state = badbit;
  813. }
  814.  
  815. int istream::skip(int i)
  816. {
  817.     int old = (_flags & ios::skipws) != 0;
  818.     if (i)
  819.     _flags |= ios::skipws;
  820.     else
  821.     _flags &= ~ios::skipws;
  822.     return old;
  823. }
  824. #endif
  825.